'use strict';

// eslint-disable-next-line jsdoc/require-param
/**
 * 更新访客用户
 */
exports.update = async ctx => {
  ctx.validate({
    user: {
      type: 'string',
      min: 4,
      max: 20,
      required: false,
    },
    pass: {
      type: 'string',
      min: 6,
      max: 20,
      required: false,
    },
    role: {
      type: 'mongo-id',
      required: false,
    },
    enabled: {
      type: 'boolean',
      required: false,
    },
  });
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  const data = await ctx.service.user.update();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 创建用户
 */
exports.create = async ctx => {
  ctx.validate({
    user: {
      type: 'string',
      min: 4,
      max: 20,
      required: true,
    },
    pass: {
      type: 'string',
      min: 6,
      max: 20,
      required: true,
    },
    role: {
      type: 'mongo-id',
      required: true,
    },
  });
  const data = await ctx.service.user.create();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 获取所有用户
 */
exports.index = async ctx => {
  const data = await ctx.service.user.all();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 删除用户
 */
exports.destroy = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  const data = await ctx.service.user.destroy();
  ctx.sendSuccess(data);
};

